#include "button_work.h"

CButtonWork *CButtonWork::currentInstance = nullptr;

CButtonWork::CButtonWork()
{
    tft = new TFT_eSPI();
    btnL = new ButtonWidget(tft);
    btnR = new ButtonWidget(tft);
    btn[0] = btnL;
    btn[1] = btnR;
    buttonCount = 2;
    currentInstance = this;
}

CButtonWork::~CButtonWork()
{
}

void CButtonWork::setup()
{
    Serial.begin(115200);
    tft->begin();
    tft->setRotation(0);
    uint16_t calData[5] = {238, 3533, 403, 3507, 4};
    tft->setTouch(calData);
    tft->fillScreen(TFT_BLACK);
    tft->setFreeFont(FF18);

    initButtons();
}

void CButtonWork::loop()
{
    
    static uint32_t scanTime = millis();
    uint16_t t_x = 9999, t_y = 9999; // To store the touch coordinates

    // Scan keys every 50ms at most
    if (millis() - scanTime >= 50)
    {
        // Pressed will be set true if there is a valid touch on the screen
        bool pressed = tft->getTouch(&t_x, &t_y);
        scanTime = millis();
        for (uint8_t b = 0; b < buttonCount; b++)
        {
            if (pressed)
            {
                if (btn[b]->contains(t_x, t_y))
                {
                    btn[b]->press(true);
                    btn[b]->pressAction();
                }
            }
            else
            {
                btn[b]->press(false);
                btn[b]->releaseAction();
            }
        }
    }
}
#define BUTTON_W 100
#define BUTTON_H 50

void CButtonWork::btnL_pressAction(void)
{
    if (currentInstance->btnL->justPressed())
    {
        Serial.println("Left button just pressed");
        currentInstance->btnL->drawSmoothButton(true);
    }
}

void CButtonWork::btnL_releaseAction(void)
{
    static uint32_t waitTime = 1000;
    if (currentInstance->btnL->justReleased())
    {
        Serial.println("Left button just released");
        currentInstance->btnL->drawSmoothButton(false);
        currentInstance->btnL->setReleaseTime(millis());
        waitTime = 10000;
    }
    else
    {
        if (millis() - currentInstance->btnL->getReleaseTime() >= waitTime)
        {
            waitTime = 1000;
            currentInstance->btnL->setReleaseTime(millis());
            currentInstance->btnL->drawSmoothButton(!currentInstance->btnL->getState());
        }
    }
}

void CButtonWork::btnR_pressAction(void)
{
    if (currentInstance->btnR->justPressed())
    {
        currentInstance->btnR->drawSmoothButton(!currentInstance->btnR->getState(), 3, TFT_BLACK, currentInstance->btnR->getState() ? "OFF" : "ON");
        Serial.print("Button toggled: ");
        if (currentInstance->btnR->getState())
            Serial.println("ON");
        else
            Serial.println("OFF");
        currentInstance->btnR->setPressTime(millis());
    }

    // if button pressed for more than 1 sec...
    if (millis() - currentInstance->btnR->getPressTime() >= 1000)
    {
        Serial.println("Stop pressing my buttton.......");
    }
    else
        Serial.println("Right button is being pressed");
}

void CButtonWork::btnR_releaseAction(void)
{
    // Not action
}

void CButtonWork::initButtons()
{
    uint16_t x = (tft->width() - BUTTON_W) / 2;
    uint16_t y = tft->height() / 2 - BUTTON_H - 10;
    btnL->initButtonUL(x, y, BUTTON_W, BUTTON_H, TFT_WHITE, TFT_RED, TFT_BLACK, "Button", 1);

    btnL->setPressAction(&CButtonWork::btnL_pressAction);
    btnL->setReleaseAction(&CButtonWork::btnL_releaseAction);
    btnL->drawSmoothButton(false, 3, TFT_BLACK); // 3 is outline width, TFT_BLACK is the surrounding background colour for anti-aliasing

    y = tft->height() / 2 + 10;
    btnR->initButtonUL(x, y, BUTTON_W, BUTTON_H, TFT_WHITE, TFT_BLACK, TFT_GREEN, "OFF", 1);
    btnR->setPressAction(btnR_pressAction);
    btnR->setReleaseAction(btnR_releaseAction);
    btnR->drawSmoothButton(false, 3, TFT_BLACK); // 3 is outline width, TFT_BLACK is the surrounding background colour for anti-aliasing
}